#define _CRT_SECURE_NO_WARNINGS
#include "Queue.h"
void QueueInit(Queue* pq) {
	assert(pq);
	pq->phead = pq->ptail = NULL;
}

void QueueDestroy(Queue* pq) {
	assert(pq);
	QueueNode* pcur = pq->phead;
	while (pcur) {
		QueueNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	pq->phead = pq->ptail = NULL;
}

void QueuePush(Queue* pq, QDataType x) {
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL) {
		perror("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->phead == NULL) {
		pq->phead = pq->ptail = newnode;
	}
	else {
		pq->ptail->next = newnode;
		pq->ptail = pq->ptail->next;
	}
}
bool QueueEmpty(Queue* pq) {
	assert(pq);
	return pq->phead == NULL;
}

void QueuePop(Queue* pq) {
	assert(pq && !QueueEmpty(pq));
	if (pq->phead == pq->ptail) {
		free(pq->phead);
		pq->phead = NULL;
		pq->ptail = NULL;
		return;
	}
	QueueNode* next = pq->phead->next;
	free(pq->phead);
	pq->phead = next;
}

QDataType QueueFront(Queue* pq) {
	assert(pq && pq->phead);
	return pq->phead->data;
}

QDataType QueueBack(Queue* pq) {
	assert(pq && pq->ptail);
	return pq->ptail->data;
}